#![allow(unused_variables)]
use sqlx::Error;
use super::CountNum;
use super::StatusBean;
use super::StatusQueryRequest;
use super::Table;

impl<'c> Table<'c, StatusBean> {
pub async fn activate_status(&self,id:i64) ->Result<u64, Error> {
		let result = sqlx::query(
	r#"update cs_statuses set active = true where id = ? "#
		)
		.bind(id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn add_status(&self,status:StatusBean) ->Result<u64, Error> {
		let result = sqlx::query(
		r#"insert into cs_statuses ( name,active) values (?,?)"#
		)
		.bind(status.name)
		.bind(status.active)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn count_active_status_records(&self) ->Result<CountNum, Error> {
		let result = sqlx::query_as(
		r#"select count(*) as countNum from cs_statuses where active = true"#
		)
		.fetch_one(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn count_search_statuses_by_fields_records(&self,status_query_request:StatusQueryRequest) ->Result<CountNum, Error> {
		let mut query = r#"select count(*) as countNum from cs_statuses where 1=1 "#.to_string();
		let name = status_query_request.name.unwrap_or_default();
		if !name.is_empty() {
			let query_name  = format!("{}{}{}"," and name like '%",name,"%' ");
			query = format!("{} {}", query, query_name);
		}
		let active = status_query_request.active.unwrap_or_default();
		if !active.is_empty() && (active=="true" || active =="false"){
			let query_active  = format!("{}{}{}"," and active = ",active," ");
			query = format!("{} {}", query, query_active);
		}
		let result = sqlx::query_as(&query)
		.fetch_one(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn delete_status(&self,id:i64) ->Result<u64, Error> {
		let result = sqlx::query(
	r#"delete from cs_statuses where id = ? "#
		)
		.bind(id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn find_status_by_id(&self,id:i64) ->Result<StatusBean, Error> {
	let result = sqlx::query_as(
	r#"select id,name,active from cs_statuses where id = ? "#
		)
		.bind(id)
		.fetch_one(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn find_status_by_name(&self,name:String) ->Result<StatusBean, Error> {
	let result = sqlx::query_as(
	r#"select id,name,active from cs_statuses where name = ?;"#
		)
		.bind(name)
		.fetch_one(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn list_active_statuses(&self) ->Result<Vec<StatusBean>, Error> {
		let result = sqlx::query_as(
	r#"select id,name,active from cs_statuses where active = true order by name asc"#
		)
		.fetch_all(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn list_all_statuses(&self) ->Result<Vec<StatusBean>, Error> {
		let result = sqlx::query_as(
	r#"select id,name,active from cs_statuses"#
		)
		.fetch_all(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn search_statuses_by_fields(&self,status_query_request:StatusQueryRequest) ->Result<Vec<StatusBean>, Error> {
		let mut query = r#"select id,name,active from cs_statuses where 1=1 "#.to_string();
		let name = status_query_request.name.unwrap_or_default();
		if !name.is_empty() {
			let query_name  = format!("{}{}{}"," and name like '%",name,"%' ");
			query = format!("{} {}", query, query_name);
		}
		let active = status_query_request.active.unwrap_or_default();
		if !active.is_empty() && (active=="true" || active =="false"){
			let query_active  = format!("{}{}{}"," and active = ",active," ");
			query = format!("{} {}", query, query_active);
		}
		let result = sqlx::query_as(&query)
		.fetch_all(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn search_statuses_by_fields_by_page(&self,status_query_request:StatusQueryRequest) ->Result<Vec<StatusBean>, Error> {
		let mut query = r#"select id,name,active from cs_statuses where 1=1 "#.to_string();
		let name = status_query_request.name.unwrap_or_default();
		if !name.is_empty() {
			let query_name  = format!("{}{}{}"," and name like '%",name,"%' ");
			query = format!("{} {}", query, query_name);
		}
		let active = status_query_request.active.unwrap_or_default();
		if !active.is_empty() && (active=="true" || active =="false"){
			let query_active  = format!("{}{}{}"," and active = ",active," ");
			query = format!("{} {}", query, query_active);
		}
		let pagenum =  status_query_request.page.unwrap_or_default();
		let pagesize =  status_query_request.rows.unwrap_or_default();
		let start = (pagenum - 1) * pagesize;
		let limit = pagesize;
		query = format!("{} limit {} offset {} ", query, limit, start);
		let result = sqlx::query_as(&query)
		.fetch_all(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn soft_delete_status(&self,id:i64) ->Result<u64, Error> {
		let result = sqlx::query(
		r#"update cs_statuses set active = false where id = ? "#
		)
		.bind(id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn toggle_status(&self,id:i64) ->Result<u64, Error> {
		let result = sqlx::query(
	r#"update cs_statuses set active = not active where id = ? "#
		)
		.bind(id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn update_status(&self,status:StatusBean) ->Result<u64, Error> {
		let result = sqlx::query(
		r#"update cs_statuses set name = ? ,active = ? where id = ? "#
		)
		.bind(status.name)
		.bind(status.active)
		.bind(status.id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn find_status_indexed_name(&self,short_name:String) ->Result<Vec<StatusBean>, Error> {
	let result = sqlx::query_as(
	r#"select id,name,active from cs_statuses where name like concat(? ,'%') "#
	)
	.bind(short_name)
	.fetch_all(&(&*self.pool).clone().unwrap())
	.await;
		return result;
}

}
